package com.terrynoya.fight.char
{
	import com.terrynoya.fight.animations.AnimationManager;
	import com.terrynoya.fight.animations.IAirInfo;
	import com.terrynoya.fight.animations.IAnimation;
	import com.terrynoya.fight.combat.EngineObject;
	import com.terrynoya.fight.combat.FightEngine;
	import com.terrynoya.fight.drawing.SpriteManager;
	import com.terrynoya.fight.emuns.Facing;
	import com.terrynoya.fight.test.CollisionEntity;
	import com.terrynoya.geom.MVector2D;

	public class Entity extends EngineObject
	{
		private var _facing:int;
		
		private var _animationManager:AnimationManager;
		private var _spriteManager:SpriteManager;
		
		private var _state:int;
		private var _physics:int;
		
		private var _position:MVector2D;
		private var _velocity:MVector2D;
		private var _acceleration:MVector2D;
		
		private var _team:int;
		
		private var _collision:CollisionEntity;
		
		public function Entity(engine:FightEngine)
		{
			super(engine);
			this._facing = Facing.RIGHT;
			this._position = new MVector2D();
			this._acceleration = new MVector2D();
			this._velocity = new MVector2D();
		}
		
		public function get collision():CollisionEntity
		{
			return _collision;
		}

		public function set collision(value:CollisionEntity):void
		{
			_collision = value;
		}
		
		public function updateInput():void
		{
			
		}

		public function get team():int
		{
			return _team;
		}

		public function set team(value:int):void
		{
			_team = value;
		}

		public function get physics():int
		{
			return _physics;
		}

		public function set physics(value:int):void
		{
			_physics = value;
		}

		public function get acceleration():MVector2D
		{
			return _acceleration;
		}

		public function set acceleration(value:MVector2D):void
		{
			_acceleration = value;
		}

		public function get velocity():MVector2D
		{
			return _velocity;
		}

		public function set velocity(value:MVector2D):void
		{
			_velocity = value;
		}

		public function get position():MVector2D
		{
			return _position;
		}

		public function set position(value:MVector2D):void
		{
			_position = value;
		}

		public function updateAnimations():void
		{
			this.animationManager.update();
		}
		
		public function setLocalAnimation(animationId:int,elementId:int):void
		{
			this.animationManager.setLocalAnimation(animationId,elementId);
		}
		
		public function updatePhysics():void
		{
			this.velocity = this.velocity.add(this.acceleration);
			this.move(this.velocity);
		}
		
		public function updateState():void
		{
			
		}
		
		public function cleanUp():void
		{
			
		}
		
		public function makeCollision():CollisionEntity
		{
			if(this.animationManager.currentElement == null)
			{
				return null;
			}
			var rlt:CollisionEntity = new CollisionEntity(this.animationManager.currentElement.clsns);
			rlt.localToGlobalCol(this.position,this.stage.height,this.facing);
			return rlt;
		}
		
		public function move(velo:MVector2D):MVector2D
		{
			var oldPos:MVector2D = this._position.clone();
			if(this.facing == Facing.RIGHT)
			{
				this.position = this.position.add(velo);
			}
			else if(this.facing == Facing.LEFT)
			{
				this.position = this.position.add(new MVector2D(-velo.x,velo.y));
			}
			
			this.bounding();
			
			var offset:MVector2D = this.position.substract(oldPos);
			return offset;
		}
		
		
		protected function bounding():void
		{
			
		}
		
		public function moveLeft(value:MVector2D):MVector2D
		{
			if(this.facing == Facing.RIGHT)
			{
				return this.move(new MVector2D(-value.x,value.y));
			}
			else if(this.facing == Facing.LEFT)
			{
				return this.move(value);	
			}
			return new MVector2D();
		}
		
		public function moveRight(value:MVector2D):MVector2D
		{
			if(this.facing == Facing.RIGHT)
			{
				return this.move(value);
			}
			else if(this.facing == Facing.LEFT)
			{
				return this.move(new MVector2D(-value.x,value.y));
			}
			return new MVector2D();
		}
		
		public function removeCheck():Boolean
		{
			return false; 
		}
		
		public function remove():void
		{
			
		}
		
		public function get currentFacing():int
		{
			return this._facing;
		}
		
		public function set currentFacing(value:int):void 
		{
			if(this._facing != value)
			{
				this.velocity = this.velocity.mutiply(new MVector2D(-1,1));
				this.acceleration = this.acceleration.mutiply(new MVector2D(-1,1));
			}
			this._facing = value;
		}
		
		public function get spriteManager():SpriteManager
		{
			return _spriteManager;
		}

		public function set spriteManager(value:SpriteManager):void
		{
			_spriteManager = value;
		}

		public function get animationManager():AnimationManager
		{
			return _animationManager;
		}

		public function set animationManager(value:AnimationManager):void
		{
			_animationManager = value;
		}

		public function draw():void
		{
			
		}
		
		public function getBasePlayer():Character
		{
			return null;
		}
		
		public function get facing():int
		{
			return _facing;
		}
		
		public function set facing(value:int):void
		{
			_facing = value;
		}
		
		public function get state():int
		{
			return _state;
		}
		
		public function set state(value:int):void
		{
			_state = value;
		}
	}
}